//
//  PictureTableViewCell.m
//  Lucky28
//
//  Created by zr on 2018/8/1.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "PictureTableViewCell.h"


@interface PictureTableViewCell ()

@property (nonatomic, strong)UIImageView * backImageView;
@property (nonatomic, strong)UIImageView * backLeftImageView;
@property (nonatomic, strong)UIImageView * backRightImageView;
@property (nonatomic, strong)UIImageView * lookNumberImageView;
@property (nonatomic, strong)UIImageView * likeBumberImageView;
@property (nonatomic, strong)UIImageView * backBottomImageView;
@property (nonatomic, strong)UILabel * lookNumberLabel;
@property (nonatomic, strong)UILabel * likeNumberLabel;
@property (nonatomic, strong)UILabel * contentLabel;
@property (nonatomic, weak)UIButton * likeButton;

@property (nonatomic, copy)NSString * ID;

@end

@implementation PictureTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

-(void)setModel:(PictureModel *)model{
    
    _model = model;
    
    NSString * str = [NSString stringWithFormat:@"%@",model.images.firstObject];

    
    NSURL * url = [NSURL URLWithString:str];
    
    [self.backImageView sd_setImageWithURL:url placeholderImage:ImageNamed(@"tufdg1")];
    self.contentLabel.text = model.title;
    self.lookNumberLabel.text = [NSString stringWithFormat:@"%@",model.comment_count];
    self.likeNumberLabel.text = [NSString stringWithFormat:@"%@",model.praise];
    self.ID = model.ID;
    
    int is_praise = [model.is_praise intValue];
    
    if (is_praise == 1) {
        
        self.likeButton.selected = YES;
    }else{
        
        self.likeButton.selected = NO;
    }
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor whiteColor];
    
    
    self.backImageView = [self setupImageViewWith:@""];
    self.backImageView.userInteractionEnabled = YES;
    
    [self.contentView addSubview:self.backImageView];
    
    self.backLeftImageView = [self setupImageViewWith:@"yuanjia"];
    
    [self.backImageView addSubview:self.backLeftImageView];
    
    self.backRightImageView = [self setupImageViewWith:@"yuanjia"];
    
    [self.backImageView addSubview:self.backRightImageView];
    
    self.backBottomImageView = [self setupImageViewWith:@"huayu"];
    
    [self.backImageView addSubview:self.backBottomImageView];
    
    self.lookNumberImageView = [self setupImageViewWith:@"yueduliang"];
    
    [self.backLeftImageView addSubview:self.lookNumberImageView];
    
    self.likeBumberImageView = [self setupImageViewWith:@"xiaozan"];
    
    [self.backRightImageView addSubview:self.likeBumberImageView];
    
    
    self.lookNumberLabel = [self setupLabelWith:@"2258"];
    
    [self.backLeftImageView addSubview:self.lookNumberLabel];
    
    
    self.likeNumberLabel = [self setupLabelWith:@"2531"];
    
    [self.backRightImageView addSubview:self.likeNumberLabel];
    
    
    self.contentLabel = [self setupLabelWith:@"奥斯福超我吃法哦爱速度和"];
    
    [self.backBottomImageView addSubview:self.contentLabel];
    
    
    UIButton * likeButton = [UIButton new];
    
    likeButton.selected = NO;
    [likeButton setImage:ImageNamed(@"weizan") forState:UIControlStateNormal];
    [likeButton setImage:ImageNamed(@"yizan") forState:UIControlStateSelected];
    [likeButton addTarget:self action:@selector(didLikeButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.likeButton = likeButton;
    
    [self.backImageView addSubview:self.likeButton];
    
    
    UIButton * hiddenButton = [UIButton new];
    
//    [hiddenButton addTarget:self action:@selector(clickHiddenButton:) forControlEvents:UIControlEventTouchUpInside];
    [hiddenButton setImage:ImageNamed(@"pingbi") forState:UIControlStateNormal];
    
    self.hiddenButton = hiddenButton;
    
    [self.contentView addSubview:self.hiddenButton];
}


-(void)layoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(10 * SCREENH6);
        make.bottom.mas_equalTo(self.contentView);
    }];
    
    [self.hiddenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backImageView).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.backImageView).offset(15 * SCREENH6);
        make.width.mas_equalTo(38 * SCREENH6);
        make.height.mas_equalTo(17.5 * SCREENH6);
    }];
    
    [self.backLeftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backImageView).offset(15 * SCREENH6);
        make.bottom.mas_equalTo(self.backImageView).offset(-10 * SCREENH6);
        make.width.mas_equalTo(55 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];
    
    [self.lookNumberImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backLeftImageView).offset(4 * SCREENH6);
        make.centerY.mas_equalTo(self.backLeftImageView);
        make.width.mas_equalTo(16 * SCREENH6);
        make.height.mas_equalTo(12 * SCREENH6);
    }];
    
    [self.lookNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.lookNumberImageView.mas_trailing).offset(3 * SCREENH6);
        make.centerY.mas_equalTo(self.lookNumberImageView);
    }];
    
    [self.backRightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(self.backLeftImageView);
        make.leading.mas_equalTo(self.backLeftImageView.mas_trailing).offset(7 * SCREENH6);
    }];
    
    [self.likeBumberImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backRightImageView).offset(4 * SCREENH6);
        make.centerY.mas_equalTo(self.backRightImageView);
        make.width.mas_equalTo(12 * SCREENH6);
        make.height.mas_equalTo(12 * SCREENH6);
    }];
    
    [self.likeNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.likeBumberImageView.mas_trailing).offset(3 * SCREENH6);
        make.centerY.mas_equalTo(self.likeBumberImageView);
    }];
    
    [self.backBottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.mas_equalTo(self.backRightImageView);
        make.leading.mas_equalTo(self.backRightImageView.mas_trailing).offset(7 * SCREENH6);
        make.width.mas_equalTo(160 * SCREENH6);//170
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backBottomImageView).offset(2 * SCREENH6);
        make.centerY.mas_equalTo(self.backBottomImageView);
    }];
    
    [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backImageView).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.backRightImageView);
    }];
}


- (UILabel *)setupLabelWith:(NSString *)string{
    
    UILabel * label = [UILabel new];
    
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:11 * SCREENH6];
    label.text = string;
    
    return label;
}

- (UIImageView *)setupImageViewWith:(NSString *)string{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = ImageNamed(string);
    
    return imageView;
}


- (void)didLikeButton:(UIButton *)button{
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSString * user_id = [userDefaults objectForKey:@"user_id"];
    NSString * token = [userDefaults objectForKey:@"token"];

    if (!button.selected) {

        NSDictionary * dict = @{@"user_id":user_id,@"note_id":self.ID,@"type":@"1",@"token":token};

        [[NetworkTool sharedTools] POST:@"/index/Note/addCollect" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            //            NSLog(@"res===%@",responseObject);
            int status = [responseObject[@"status"] intValue];

            if (status == 200) {
                button.selected = YES;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"GetPictureData" object:nil];
            }else{

                NSString * string = [NSString stringWithFormat:@"%@",responseObject[@"msg"]];
                if (self.delegate && [self.delegate respondsToSelector:@selector(didCellLikeButton:)]) {
                    [self.delegate didCellLikeButton:string];
                }
            }

        }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        }];

    }else{
        
        NSDictionary * dict = @{@"user_id":user_id,@"note_id":self.ID,@"type":@"1",@"token":token,@"is_cancel":@"1"};
        
        [[NetworkTool sharedTools] POST:@"/index/Note/addCollect" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            //            NSLog(@"res===%@",responseObject);
            int status = [responseObject[@"status"] intValue];
            
            if (status == 200) {
                button.selected = NO;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"GetPictureData" object:nil];
            }else{
                
                NSString * string = [NSString stringWithFormat:@"%@",responseObject[@"msg"]];
                if (self.delegate && [self.delegate respondsToSelector:@selector(didCellLikeButton:)]) {
                    [self.delegate didCellLikeButton:string];
                }
            }
            
        }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
